package com.tanhua.server.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.server.api.QuanZiApi;
import com.tanhua.dubbo.server.pojo.Comment;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.server.pojo.User;
import com.tanhua.server.pojo.UserInfo;
import com.tanhua.server.utils.UserThreadLocal;
import com.tanhua.server.vo.Comments;
import com.tanhua.server.vo.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CommentsService {

    @Reference(version = "1.0.0")
    private QuanZiApi quanZiApi;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 查询评论
     *
     * @param publishId
     * @param page
     * @param pagesize
     * @return
     */
    public PageResult queryCommentsList(String publishId, Integer page, Integer pagesize) {
        User user = UserThreadLocal.get();

        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pagesize);
        pageResult.setPages(0);
        pageResult.setCounts(0);

        PageInfo<Comment> pageInfo = quanZiApi.queryCommentList(publishId, page, pagesize);
        //获取comment集合
        List<Comment> records = pageInfo.getRecords();

        if (CollectionUtils.isEmpty(records)) {
            return pageResult;
        }


        //定义集合封装用户id
        List<Long> userIds = new ArrayList<>();
        for (Comment record : records) {
            if (!userIds.contains(record.getUserId())) {
                userIds.add(record.getUserId());
            }
        }

        //查询用户信息表
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.in("user_id", userIds);
        List<UserInfo> userInfoList = userInfoService.queryUserInfoList(wrapper);

        //创建集合存储comment对象
        List<Comments> commentsList = new ArrayList<>();

        for (Comment record : records) {
            Comments comments = new Comments();
            comments.setId(record.getId().toHexString());
            comments.setCreateDate(new DateTime(record.getCreated()).toString("yyyy年MM月dd日 HH:mm"));  //评论时间
            comments.setContent(record.getContent());  //评论内容

            for (UserInfo userInfo : userInfoList) {
                if (userInfo.getUserId().longValue() == record.getUserId().longValue())
                    comments.setAvatar(userInfo.getLogo());
                comments.setNickname(userInfo.getNickName());
                break;
            }

            //从redis中获取点赞数
            String likeCommentKey = "QUANZI_COMMENT_LIKE_" + comments.getId();
            String likeCommentValue = (String) this.redisTemplate.opsForValue().get(likeCommentKey);

            //如果从redis中获取的点赞数为空,则默认赋值为0
            if (StringUtils.isNotEmpty(likeCommentValue)) {
                comments.setLikeCount(Integer.valueOf(likeCommentValue));  //TODO 点赞数
            } else {
                comments.setLikeCount(0);
            }


            String likeUserKey = "QUANZI_COMMENT_LIKE_USER_" + user.getId() + "_" + comments.getId();
            comments.setHasLiked(redisTemplate.hasKey(likeUserKey) ? 1 : 0);   //TODO 是否点赞(1是 , 0否)

            commentsList.add(comments);
        }

        pageResult.setItems(commentsList);

        return pageResult;
    }

    /**
     * 发表评论
     *
     * @param publishId
     * @param content
     * @return
     */
    public Boolean saveComments(String publishId, String content) {

        //获取用户信息
        User user = UserThreadLocal.get();
        return quanZiApi.saveComment(user.getId(), publishId, 2, content);

    }

}
